package com.yenlien.traceyun.business.controller;


import com.yenlien.traceyun.business.entity.Unit;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.service.UnitService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by LiuSha on 2020/6/16.
 */
@RestController
@RequestMapping("/unit")
public class UnitController extends BaseController {
    @Autowired
    private UnitService unitService;

    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody Unit unit) {
        unit.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        return buildPage(unitService.findByList(unit));
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@RequestBody Unit unit) {
        unit.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        unitService.save(unit);
        return success("success");
    }

    @RequestMapping(value = "/saves", method = RequestMethod.POST)
    public Object saves(@RequestBody List<Unit> units) {
        if (units.size() > 0) {
            for (Unit unit : units) {
                unit.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
                unitService.save(unit);
            }
        }
        return success("success");
    }

    @RequestMapping(value = "/findByAll", method = RequestMethod.GET)
    public Object findByAll() {
        return success(unitService.findByAll(UserAuthUtil.getLoginUser().getCompanyId()));
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(@RequestBody Unit unit) {
        unitService.update(unit);
        return success("success");
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable String id) {
        return success(unitService.findById(id));
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Object delete(@PathVariable String id) {
        unitService.delete(id);
        return success("success");
    }
}
